package com.moxiao.yfjh.module.blind.controller.admin.comment;

import com.moxiao.yfjh.framework.common.pojo.CommonResult;
import com.moxiao.yfjh.framework.common.pojo.PageResult;
import com.moxiao.yfjh.framework.excel.core.util.ExcelUtils;
import com.moxiao.yfjh.framework.operatelog.core.annotations.OperateLog;
import com.moxiao.yfjh.module.blind.controller.admin.comment.vo.*;
import com.moxiao.yfjh.module.blind.convert.comment.MemberCommentConvert;
import com.moxiao.yfjh.module.blind.dal.dataobject.comment.MemberCommentDO;
import com.moxiao.yfjh.module.blind.dal.dataobject.member.BlindMemberUserDO;
import com.moxiao.yfjh.module.blind.service.comment.MemberCommentService;
import com.moxiao.yfjh.module.blind.service.member.BlindMemberUserService;
import com.moxiao.yfjh.module.system.dal.dataobject.user.AdminUserDO;
import com.moxiao.yfjh.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Objects;

import static com.moxiao.yfjh.framework.common.pojo.CommonResult.success;
import static com.moxiao.yfjh.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;
import static com.moxiao.yfjh.framework.security.core.util.SecurityFrameworkUtils.getLoginUserId;

@Tag(name = "管理后台 - 相亲会员评价")
@RestController
@RequestMapping("/blind/member-comment")
@Validated
public class MemberCommentController {

    @Resource
    private MemberCommentService memberCommentService;
    @Resource
    private BlindMemberUserService memberUserService;
    @Resource
    private AdminUserService adminUserService;


    @PutMapping("/update")
    @Operation(summary = "更新相亲会员评价")
    @PreAuthorize("@ss.hasPermission('blind:member-comment:update')")
    public CommonResult<Boolean> updateMemberComment(@Valid @RequestBody MemberCommentUpdateReqVO updateReqVO) {
        Long loginUserId = getLoginUserId();
        updateReqVO.setAuitUser(String.valueOf(loginUserId));
        updateReqVO.setAuitTime(LocalDateTime.now());
        memberCommentService.updateMemberComment(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除相亲会员评价")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('blind:member-comment:delete')")
    public CommonResult<Boolean> deleteMemberComment(@RequestParam("id") Long id) {
        memberCommentService.deleteMemberComment(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得相亲会员评价")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('blind:member-comment:query')")
    public CommonResult<MemberCommentRespVO> getMemberComment(@RequestParam("id") Long id) {
        MemberCommentDO memberComment = memberCommentService.getMemberComment(id);
        MemberCommentRespVO memberCommentRespVO = MemberCommentConvert.INSTANCE.convert(memberComment);
        List<Long> list = new ArrayList<>();
        list.add(memberComment.getUserId());
        list.add(Long.valueOf(memberComment.getMomentUser()));
        List<BlindMemberUserDO> memberUserList = memberUserService.getMemberUserList(list);
        if (CollectionUtils.isNotEmpty(memberUserList)){
            memberUserList.forEach(blindMemberUserDO -> {
                if (blindMemberUserDO.getId().equals(memberComment.getUserId())){
                    memberCommentRespVO.setCommentName(blindMemberUserDO.getNickname());
                    memberCommentRespVO.setAvatar(blindMemberUserDO.getAvatar());
                    memberCommentRespVO.setNickname(blindMemberUserDO.getNickname());
                }else {
                    memberCommentRespVO.setBeingCommentedName(blindMemberUserDO.getNickname());
                }
            });
        }
        // 评论人
        if (Objects.nonNull(memberComment.getUserId())) {
            BlindMemberUserDO userDO = memberUserService.getMemberUser(memberComment.getUserId());
            if (Objects.nonNull(userDO)) {
                memberCommentRespVO.setUserName(userDO.getNickname());
                memberCommentRespVO.setUserNo(userDO.getUserNo());
            }
        }
        // 被评论人
        if (Objects.nonNull(memberComment.getMomentUser())) {
            BlindMemberUserDO memberUserDO = memberUserService.getMemberUser(Long.valueOf(memberComment.getMomentUser()));
            if (Objects.nonNull(memberUserDO)) {
                memberCommentRespVO.setMomentUserName(memberUserDO.getNickname());
                memberCommentRespVO.setMomentUserNo(memberUserDO.getUserNo());
            }
        }
        //审核人
        if (StringUtils.isNotBlank(memberComment.getAuitUser())) {
            AdminUserDO auitUserDo = adminUserService.getUser(Long.valueOf(memberComment.getAuitUser()));
            if (Objects.nonNull(auitUserDo)) {
                memberCommentRespVO.setAuitUserName(auitUserDo.getNickname());
            }
        }

        return success(memberCommentRespVO);
    }

    @GetMapping("/list")
    @Operation(summary = "获得相亲会员评价列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('blind:member-comment:query')")
    public CommonResult<List<MemberCommentRespVO>> getMemberCommentList(@RequestParam("ids") Collection<Long> ids) {
        List<MemberCommentDO> list = memberCommentService.getMemberCommentList(ids);
        return success(MemberCommentConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得相亲会员评价分页")
    @PreAuthorize("@ss.hasPermission('blind:member-comment:query')")
    public CommonResult<PageResult<MemberCommentRespVO>> getMemberCommentPage(@Valid MemberCommentPageReqVO pageVO) {
        PageResult<MemberCommentDO> pageResult = memberCommentService.getMemberCommentPage(pageVO);
        PageResult<MemberCommentRespVO> memberCommentRespVOPageResult = MemberCommentConvert.INSTANCE.convertPage(pageResult);
        if (Objects.nonNull(memberCommentRespVOPageResult)) {
            List<MemberCommentRespVO> memberCommentRespVOS = memberCommentRespVOPageResult.getList();
            if (CollectionUtils.isNotEmpty(memberCommentRespVOS)) {
                memberCommentRespVOS.forEach(memberCommentRespVO -> {
                    List<Long> list = new ArrayList<>();
                    list.add(memberCommentRespVO.getUserId());
                    list.add(Long.valueOf(memberCommentRespVO.getMomentUser()));
                    List<BlindMemberUserDO> memberUserList = memberUserService.getMemberUserList(list);
                    if (CollectionUtils.isNotEmpty(memberUserList)){
                        memberUserList.forEach(blindMemberUserDO -> {
                            if (blindMemberUserDO.getId().equals(memberCommentRespVO.getUserId())){
                                memberCommentRespVO.setCommentName(blindMemberUserDO.getNickname());
                                memberCommentRespVO.setAvatar(blindMemberUserDO.getAvatar());
                                memberCommentRespVO.setNickname(blindMemberUserDO.getNickname());
                            }else {
                                memberCommentRespVO.setBeingCommentedName(blindMemberUserDO.getNickname());
                            }
                        });
                    }

                });
            }
        }
        return success(memberCommentRespVOPageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出相亲会员评价 Excel")
    @PreAuthorize("@ss.hasPermission('blind:member-comment:export')")
    @OperateLog(type = EXPORT)
    public void exportMemberCommentExcel(@Valid MemberCommentExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MemberCommentDO> list = memberCommentService.getMemberCommentList(exportReqVO);
        // 导出 Excel
        List<MemberCommentExcelVO> datas = MemberCommentConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "相亲会员评价.xls", "数据", MemberCommentExcelVO.class, datas);
    }

}
